package cn.tedu.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.tedu.dao.UserDao;
import cn.tedu.entity.User;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//在第一次调用getParmeter之前设置
		request.setCharacterEncoding("utf-8");
		String user=request.getParameter("user");
		String pwd=request.getParameter("pwd");
		//利用数据层获取用户信息
		UserDao dao=new UserDao();
		User usr=dao.finUserByName(user);
		String msg="";
		if(usr==null) {
			msg="用户名不存在";
		}else if(usr.getPassword().equals(pwd)) {
			//登录成功,将用户信息储存到Session
			//用于与其他Servlet共享登录状态
			HttpSession session=request.getSession();
			session.setAttribute("loginUser", usr);
			msg="登录成功";
		}else {
			msg="登录失败";
		}
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().println(msg);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
